package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

// Created on:	2-Jun-08
// Author:		Richard Shih

public class SystemSetting
{

	private String	name;
	private String	value;
	private int 	_newSysMaxLoad;
	private int 	_newBillingPeriod;
	private String	msg = "";

	public SystemSetting()
	{
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		this.value = value;
	}
	
	public void setNewSysMaxLoad(int newSysMaxLoad_)
	{
		this._newSysMaxLoad = newSysMaxLoad_;
	}

	public int getNewSysMaxLoad()
	{
		return this._newSysMaxLoad;
	}

	public void setNewBillingPeriod(int newBillingPeriod_)
	{
		this._newBillingPeriod = newBillingPeriod_;
	}

	public int getNewBillingPeriod()
	{
		return this._newBillingPeriod;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	private Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Class.forName("org.sqlite.JDBC");
		String home = System.getProperty("user.home");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");

		return conn;
	}
	
	public int getCurrentMaxLoad()
	{
		try{
			Connection conn = getConnection();
			PreparedStatement ps;
			
			ps = conn.prepareStatement("select value from system where name = 'CallLoad'");
			ResultSet rs = ps.executeQuery();
			
			if(rs.next())
			{
				int i = Integer.parseInt(rs.getString(1));
				rs.close();
				return i;
			} 
			else
			{
				rs.close();
				return 0;
			}
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in System Settings");
			return -2;
		}
	}
	
	public boolean setCurrentMaxLoad()
	{
		try{
			Connection conn = getConnection();
			Statement stmt = conn.createStatement();
			
			String q = "select name from system where name = 'CallLoad'";
			ResultSet rs = stmt.executeQuery(q);
			
			if (rs.next()){
				q = "update system set value = '" + this._newSysMaxLoad + "' where name = 'CallLoad'";
				stmt.executeUpdate(q);	
			} else {
				q = "insert into system values ('CallLoad', '" + this._newSysMaxLoad + "' )";
				stmt.executeUpdate(q);
			}
			
			rs.close();
			conn.close();
						
			Server.changeCallLoad(this._newSysMaxLoad);
			
			return true;		
			
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in SystemSettings - setCurrentMaxLoad()");
			this.msg = "Error%20setting%20maximum%20system%20load";
			return false;
		}
		
	}
	
	public int getCurrentBillingPeriod()
	{
		try{
			Connection conn = getConnection();
			PreparedStatement ps;
			
			ps = conn.prepareStatement("select value from system where name = 'BillingPeriod'");
			ResultSet rs = ps.executeQuery();
			
			if(rs.next())
			{
				int i = Integer.parseInt(rs.getString(1));
				rs.close();
				return i;
			} 
			else
			{
				rs.close();
				return 0;
			}
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in System Settings");
			return -2;
		}
	}
	
	public boolean setCurrentBillingPeriod()
	{
		try{
			Connection conn = getConnection();
			Statement stmt = conn.createStatement();

			String q = "select * from system where name = 'BillingPeriod'";
			ResultSet rs = stmt.executeQuery(q);
			
			if (rs.next()){
				q = "update system set value = '" + this._newBillingPeriod + "' where name = 'BillingPeriod'";
				stmt.executeUpdate(q);	
			} else {
				q = "insert into system values ('BillingPeriod', '" + this._newBillingPeriod + "')";
				stmt.executeUpdate(q);
			}
			
			rs.close();
			conn.close();
						
			Server.changeBillingPeriod(this._newBillingPeriod);
			
			return true;		
			
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in SystemSettings - setCurrentBillingPeriod()");
			this.msg = "Error%20changing%20billing%20period";
			System.out.println(e);
			return false;
		}
		
	}

	public boolean resetSystem()
	{
		return Server.resetPhoneSystem();
	}
	
}
